package com.castle.fortress.admin.study.course.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.study.course.entity.StudyCourseClassifyEntity;
import com.castle.fortress.admin.study.course.dto.StudyCourseClassifyDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.study.course.service.StudyCourseClassifyService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;

/**
 * 课程/课件分类表 api 控制器
 *
 * @author castle
 * @since 2023-01-30
 */
@Api(tags="课程/课件分类表api管理控制器")
@RestController
@RequestMapping("/api/study/course/studyCourseClassify")
public class ApiStudyCourseClassifyController {
    @Autowired
    private StudyCourseClassifyService studyCourseClassifyService;


    /**
     * 课程/课件分类表的树形展示
     * @param studyCourseClassifyDto 课程/课件分类表实体类
     * @return
     */
    @CastleLog(operLocation="课程/课件分类表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("课程/课件分类表树形展示")
    @GetMapping("/tree")
    public RespBody<List<StudyCourseClassifyDto>> treeStudyCourseClassify(StudyCourseClassifyDto studyCourseClassifyDto){
	    List<StudyCourseClassifyDto> list = studyCourseClassifyService.listStudyCourseClassify(studyCourseClassifyDto);
	    //展示时需要展示到顶级
	    Set<Long> ids=new HashSet<>();
	    for(StudyCourseClassifyDto s:list){
		    ids.add(s.getId());
	    }
	    List<Long> parentId =new ArrayList<>();
	    List<StudyCourseClassifyDto> parentList=new ArrayList<>();
	    for(StudyCourseClassifyDto s:list){
		    if(s.getParentId()!=null&&!ids.contains(s.getParentId())){
			    parentId.add(s.getParentId());
		    }
	    }
	    if(!parentId.isEmpty()){
		    QueryWrapper<StudyCourseClassifyEntity> wrapper=new QueryWrapper<>();
		    wrapper.in("id",parentId);
			wrapper.orderByDesc("sort");
		    parentList= ConvertUtil.transformObjList(studyCourseClassifyService.list(wrapper),StudyCourseClassifyDto.class);
		    list.addAll(parentList);
	    }
	    do{
		    parentId =new ArrayList<>();
		    for(StudyCourseClassifyDto s:parentList){
			    if(s.getParentId()!=null){
				    parentId.add(s.getParentId());
			    }
		    }
		    if(!parentId.isEmpty()){
			    QueryWrapper<StudyCourseClassifyEntity> wrapper=new QueryWrapper<>();
			    wrapper.in("id",parentId);
				wrapper.orderByDesc("sort");
			    parentList= ConvertUtil.transformObjList(studyCourseClassifyService.list(wrapper),StudyCourseClassifyDto.class);
			    list.addAll(parentList);
		    }
	    }while(!parentId.isEmpty());
	    return RespBody.data(ConvertUtil.listToTree(list));
    }

    /**
     * 课程/课件分类表保存
     * @param studyCourseClassifyDto 课程/课件分类表实体类
     * @return
     */
    @CastleLog(operLocation="课程/课件分类表",operType = OperationTypeEnum.INSERT)
    @ApiOperation("课程/课件分类表保存")
    @PostMapping("/save")
    public RespBody<String> saveStudyCourseClassify(@RequestBody StudyCourseClassifyDto studyCourseClassifyDto){
	    if(studyCourseClassifyDto == null ){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
        StudyCourseClassifyEntity studyCourseClassifyEntity = ConvertUtil.transformObj(studyCourseClassifyDto,StudyCourseClassifyEntity.class);
	    if(studyCourseClassifyEntity.getParentId()!=null){
              StudyCourseClassifyEntity parent = studyCourseClassifyService.getById(studyCourseClassifyEntity.getParentId());
		    if(StrUtil.isEmpty(parent.getParents())){
                studyCourseClassifyEntity.setParents(parent.getId()+"");
		    }else{
                studyCourseClassifyEntity.setParents(parent.getParents()+","+parent.getId());
		    }
	    }
	    if(studyCourseClassifyService.save(studyCourseClassifyEntity)){
		    return RespBody.data("保存成功");
	    }else{
		    return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
	    }
    }

    /**
     * 课程/课件分类表编辑
     * @param studyCourseClassifyDto 课程/课件分类表实体类
     * @return
     */
    @CastleLog(operLocation="课程/课件分类表",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("课程/课件分类表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateStudyCourseClassify(@RequestBody StudyCourseClassifyDto studyCourseClassifyDto){
	    if(studyCourseClassifyDto == null || studyCourseClassifyDto.getId() == null || studyCourseClassifyDto.getId().equals(0L)){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
            StudyCourseClassifyEntity studyCourseClassifyEntity = ConvertUtil.transformObj(studyCourseClassifyDto,StudyCourseClassifyEntity.class);
	    if(studyCourseClassifyEntity.getParentId()!=null){
              StudyCourseClassifyEntity parent = studyCourseClassifyService.getById(studyCourseClassifyEntity.getParentId());
		    if(StrUtil.isEmpty(parent.getParents())){
                studyCourseClassifyEntity.setParents(parent.getId()+"");
		    }else{
                studyCourseClassifyEntity.setParents(parent.getParents()+","+parent.getId());
		    }
	    }
	    if(studyCourseClassifyService.updateById(studyCourseClassifyEntity)){
		    return RespBody.data("保存成功");
	    }else{
		    return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
	    }
    }

    /**
     * 课程/课件分类表删除
     * @param ids 课程/课件分类表id集合
     * @return
     */
    @CastleLog(operLocation="课程/课件分类表",operType = OperationTypeEnum.DELETE)
    @ApiOperation("课程/课件分类表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteStudyCourseClassify(@RequestBody List<Long> ids){
	    if(ids == null || ids.size()<1){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
	    QueryWrapper<StudyCourseClassifyEntity> wrapper=new QueryWrapper<>();
	    wrapper.in("id",ids);
	    for(Long d:ids){
		    wrapper.or().like("parents",d);
	    }
	    if(studyCourseClassifyService.remove(wrapper)) {
		    return RespBody.data("删除成功");
	    }else{
		    return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
	    }
    }

    /**
     * 课程/课件分类表详情
     * @param id 课程/课件分类表id
     * @return
     */
    @CastleLog(operLocation="课程/课件分类表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("课程/课件分类表详情")
    @GetMapping("/info")
    public RespBody<StudyCourseClassifyDto> infoStudyCourseClassify(@RequestParam Long id){
	    if(id == null){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
                StudyCourseClassifyDto studyCourseClassifyDto =  studyCourseClassifyService.getByIdExtends(id);

	    return RespBody.data(studyCourseClassifyDto);
    }


}
